package com.neuedu.elmserver.controller;


import com.neuedu.elmserver.pojo.Users;
import com.neuedu.elmserver.service.UsersService;
import com.neuedu.elmserver.vo.ChangePasswordRequest;
import com.neuedu.elmserver.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/users")
@CrossOrigin //解决跨域
public class UsersController {

    @Autowired
    private UsersService usersService; //UsersController依赖UsersService

    /**
     * API数据接口：查询所有用户信息
     * @return
     */
    @GetMapping("/listUser")
    public List<Users> listUser(){
        return usersService.queryAllUsers();
    }

    @GetMapping("/login")
    public ResultVO login(String username, String password){
        return usersService.userLogin(username,password);
    }

    // 注册接口
    @PostMapping("/register")
    public ResultVO register(@RequestBody Users user) {
        return usersService.userRegister(user.getUserName(), user.getPassword());
    }

    @PostMapping("/changePassword")
    public ResultVO changePassword(@RequestBody ChangePasswordRequest request) {
        return usersService.changePassword(request.getUsername(), request.getCurrentPassword(), request.getNewPassword());
    }

    //个人信息
    @CrossOrigin(origins = "http://localhost:5173")
    @GetMapping("/profile")
    public ResultVO profile(@RequestParam String username){
        return usersService.userProfile(username);
    }

    // 更新用户信息
    @CrossOrigin(origins = "http://localhost:5173")
    @PutMapping("/update")
    public ResultVO updateUserProfile(@RequestBody Users user) {
        return usersService.updateUserProfile(user);
    }

    @PostMapping("/beVIP")
    public ResultVO beVIP(@RequestBody Users user){
        return usersService.beVIP(user);
    }
}
